package com.libraries.helper;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 压缩工具类
 * @author wellsa
 *
 */
public class CompressHelper {

	private static final String TAG = "CompressHelper";

	/**
	 * 
	 * @param zipFileName
	 * @param inputFile
	 * @return
	 */
	public static boolean compression(String zipFileName, File inputFile){
		Logger.log(TAG, "compression", "压缩中...");
		try {
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
					zipFileName));
			BufferedOutputStream bo = new BufferedOutputStream(out);
			doCompression(out, inputFile, inputFile.getName(), bo);
			bo.close();
			out.close(); // 输出流关闭
			Logger.log(TAG, "compression", "压缩成功");
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			Logger.log(TAG, "compression err", e);
		} catch (Exception e) {
			e.printStackTrace();
			Logger.log(TAG, "compression err", e);
		}
		Logger.log(TAG, "compression", "压缩失败");
		return false;
	}

	private static void doCompression(ZipOutputStream out, File file, String base,
			BufferedOutputStream bo) throws Exception { // 方法重载
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			if (files.length == 0) {
				out.putNextEntry(new ZipEntry(base + "/")); // 创建zip压缩进入点base
			}
			for (int vari = 0;vari< files.length;vari++) {
				doCompression(out, files[vari], base + "/" + files[vari].getName(), bo); // 递归遍历子文件夹
			}
		} else {
			out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入点base
			FileInputStream in = new FileInputStream(file);
			BufferedInputStream bi = new BufferedInputStream(in);
			int b;
			while ((b = bi.read()) != -1) {
				bo.write(b); // 将字节流写入当前zip目录
			}
			bi.close();
			in.close(); // 输入流关闭
		}
	}

	/**
	 * 解压文件
	 * 
	 * @param zipfile
	 * @param destToPath
	 *            输出路径（文件夹目录）
	 * @throws IOException
	 */
	public static boolean decompression(File zipfile, String destToPath)
			throws IOException {
		Logger.log(TAG, "decompression", zipfile.getName(), destToPath);
		try {
			ZipInputStream zinStream = new ZipInputStream(new FileInputStream(
					zipfile));// 输入源zip路径
			BufferedInputStream binStream = new BufferedInputStream(zinStream);
			File Fout = null;
			ZipEntry entry;
			try {
				while ((entry = zinStream.getNextEntry()) != null
						&& !entry.isDirectory()) {
					Fout = new File(destToPath, entry.getName());
					if (!Fout.exists()) {
						(new File(Fout.getParent())).mkdirs();
					}
					FileOutputStream outStream = new FileOutputStream(Fout);
					BufferedOutputStream boutStream = new BufferedOutputStream(
							outStream);
					int b;
					while ((b = binStream.read()) != -1) {
						boutStream.write(b);
					}
					boutStream.close();
					outStream.close();
				}
				Logger.log(TAG, "decompression~", "解压成功");
				binStream.close();
				zinStream.close();
				return true;
			} catch (IOException e) {
				Logger.log(TAG, "decompression err", e);
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			Logger.log(TAG, "decompression err", e);
		}
		return false;
	}

}
